<?php

include_once '../Interfaces/XMLInterface.php';
include_once '../AtacCommunicator/ACBusWaitingTimes.php';
include_once '../InfoManager/InfoManager.php';
include_once '../Exceptions/WrongParametersException.php';
include_once '../Exceptions/MuchParametersException.php';

/**
 * La classe StepAtRoute rappresenta ogni step del percorso
 * @author Laura Magrini
 *
 */

class StepAtRoute implements XMLInterface {
	private $elementStart;
	private $elementEnd;
	
	private $step;
	private $description;
	private $palina;
	private $tempo;
	private $metri;
	private $stopID;
	private $busLine;
	private $fermata;
	private $pathid;
	private $direzione;
	
	function __construct($step, $description, $palina=null, $tempo=null, $metri=null, $stopID=null) {
		$this->step = $step;
		$this->description = $description;
		$this->palina = $palina;
		$this->tempo = $tempo;
		$this->metri = $metri;
		$this->stopID = $stopID; //id della fermata per le paline
		
		$this->elementStart = "\n<PLACEMARK>";
		$this->elementEnd = "</PLACEMARK>\n";
	}
	
	function getStep() {
		return $this->step;
	}
	
	function getDesc() {
		return $this->description;
	}
	
	function setDesc($desc) {
		$this->description=$desc;
	}
	
	function getStopID() {
		return $this->stopID;
	}
	
		
	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementStartTag()
	 */
	function getElementStartTag() {
		return $this->elementStart;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see src/Interfaces/XMLInterface#getElementEndTag()
	 */
	function getElementEndTag() {
		return $this->elementEnd;
	}
	
	function getPathID() {
		$bwt = new ACRoutePlanner();
		return $bwt->getPathIDForLineNumber($this->busLine, $this->direzione);
	}
	
	function getStopIDFromAtac($descrizione) {
		$bwt = new ACRoutePlanner();
		return $bwt->getStopIDForLineNumber($this->pathid, $descrizione);
	}
	
	//metodo per estrarre info aggiuntive dalla desrizione
	function getOtherInfo($desc){
		$app = explode("Prendere la linea: ",$desc);
		$temp= explode("(",$app[1],2);
		$temp= explode(")",$temp[1]);
		
		if (strpos($temp[0], "(") === false){
			$this->direzione=$temp[0];	
		}
		else $this->direzione=$temp[0].")";
		
		//$this->direzione=$temp[0];
		$temp= explode("fermat",$app[1]);
		$temp= explode("per ",$temp[0]);
		$this->fermata=substr($temp[1],0,strlen($temp[1])-1);
		$app = explode(" ",$app[1]);
		$this->busLine=$app[0];
							
		unset($app);
		unset($temp);
		return;
	}
	
	
	function toXML($mode) {		
		$xml = "";
		$descrizione=null;		
		$palina=null;
		if ($mode==COMPACT_MODE) {
			$xml = "<STEP>".$this->step."</STEP>\n";
			if(($this->description!=null) && (count($this->description)>0)){
				$xml = $xml."<LISTDESC>\n";
				for($i=0; $i<count($this->description); $i++){					
						$xml = $xml."<DESCRIPTION>".$this->description[$i]."</DESCRIPTION>\n";
						if (stristr($this->description[$i], "Prendere la linea: ")){
							$app = explode("Prendere la linea: ",$this->description[$i]);
							$app = explode(" ",$app[1]);
							$fermata=$app[0];						
						}
				}
				if ($this->palina!=null){ //se è presente Palina prendo solo i dati che la precedono e la seguono
							$xml = $xml."<PALINA>".$this->palina."</PALINA>\n";
				}$xml = $xml."</LISTDESC>\n";
			}
		}
		
		if ($mode==EXTENDED_MODE) {
			$xml = "<STEP>".$this->step."</STEP>\n";
			if(($this->description!=null) && (count($this->description)>0)){
				$xml = $xml."<LISTDESC>\n";
				for($i=0; $i<count($this->description); $i++){					
						$xml = $xml."<DESCRIPTION>".$this->description[$i]."</DESCRIPTION>\n";
						if (stristr($this->description[$i], "Prendere la linea: ")){
													
							$this->getOtherInfo($this->description[$i]);
							
						}	
				}
				//visualizzo tutte le informazioni sull'autobus da prendere
				if($this->busLine!=null && $this->busLine!="METROA" && $this->busLine!="METROB")	{
						$this->pathid=$this->getPathID();
						if($this->stopID=="" || $this->stopID==null){
							$this->stopID=$this->getStopIDFromAtac($this->step);
						}						
						$xml = $xml."<INFOBUS>\n";
						$xml = $xml."<PALINA>".$this->palina."</PALINA>\n";
						$xml = $xml."<BUSLINE>".$this->busLine."</BUSLINE>\n";
						$xml = $xml."<DIRECTION>".$this->direzione."</DIRECTION>\n";
						$xml = $xml."<FERMATE>".$this->fermata."</FERMATE>\n";
						$xml = $xml."<PATHID>".$this->pathid."</PATHID>\n"; 
						$xml = $xml."<STOPID>".$this->stopID."</STOPID>\n";
						$xml = $xml."</INFOBUS>\n";
				}
				$xml = $xml."</LISTDESC>\n";
			}	
		}
		
		$xml = $this->elementStart."\n".$xml."".$this->elementEnd."\n";
		if ($this->tempo!=null) 
					$xml = $xml."<TEMPO>".$this->tempo."</TEMPO>\n";
		if ($this->metri!=null) 
					$xml = $xml."<METRI>".$this->metri."</METRI>\n"; 
		return $xml;
	}
	
}